﻿using LeakView.common;
using LeakView.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LeakView.misc.view
{
    class MiscGridView : DataGridView
    {

        private DataTable dataTable = new DataTable();

        public DataTable Table { get => dataTable; }

        public MiscGridView()
        {
            //initialize();
        }

        public void initialize()
        {
            this.AllowUserToResizeRows = false;
            this.AllowUserToResizeColumns = true;
            this.AllowUserToOrderColumns = false;
            this.AllowUserToAddRows = false;
            this.EditMode = DataGridViewEditMode.EditProgrammatically;
            this.DefaultCellStyle.SelectionBackColor = this.DefaultCellStyle.BackColor;
            this.DefaultCellStyle.SelectionForeColor = this.DefaultCellStyle.ForeColor;

            this.RowHeadersVisible = false;


            dataTable.Columns.Add("时间");
            dataTable.Columns.Add("数值");
            this.DataSource = dataTable;

            for (int i = 0; i < this.Columns.Count; i++)
            {
                this.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            }

            this.DataSource = dataTable;

            this.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 9, FontStyle.Regular);
            this.ColumnHeadersDefaultCellStyle.BackColor = Color.Gray;
            this.ColumnHeadersHeight = 32;
            this.RowsDefaultCellStyle.Font = new Font("微软雅黑", 9, FontStyle.Regular);

        }

        public void addValues(List<AjaxMiscValue> values)
        {
            values.ForEach(v =>
            {
                this.addValue(v);
            });

        }

        public void addValue(AjaxMiscValue value)
        {
            addValue(value.birth, value.value);
        }

        public void addValue(long timestamp, double value)
        {
            DateTime dt = Tools.ConvertLongToDateTime(timestamp);

            DataRow dr = Table.NewRow();
            dr.SetField(0, dt.ToString("yyyy-MM-dd HH:mm:ss"));

            if (double.IsNaN(value))
                dr.SetField(1, "");
            else
                dr.SetField(1, value);

            Table.Rows.Add(dr);

        }

        public void clearValues()
        {
            Table.Rows.Clear();
        }

    }
}
